package cn.huang.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import cn.huang.dao.AddressDao;
import cn.huang.entity.PageResult;
import cn.huang.entity.QueryPageBean;
import cn.huang.pojo.Address;
import cn.huang.service.AddressService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import redis.clients.jedis.JedisPool;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service(interfaceClass = AddressService.class)
@Transactional
public class AddressServiceImpl implements AddressService {
    @Autowired
    private AddressDao addressDao;
    @Autowired
    private JedisPool jedisPool;

    @Override
    public void add(Address address) {
        addressDao.add(address);
    }

    @Override
    public PageResult findPage(QueryPageBean queryPageBean) {
        if (queryPageBean.getQueryString() != null && !queryPageBean.getQueryString().equals("")){
            queryPageBean.setQueryString("%"+queryPageBean.getQueryString()+ "%");
        }
        List<Address> orderList = addressDao.queryPage( queryPageBean.getQueryString());
        if (orderList != null){
            if ( orderList.size() < 10 || queryPageBean.getCurrentPage() >= Math.round(Math.ceil((double)orderList.size()/10))){
                queryPageBean.setCurrentPage(1);
            }
        }
        PageHelper.startPage(queryPageBean.getCurrentPage(), queryPageBean.getPageSize());
        Page<Address> pages = addressDao.queryPage(queryPageBean.getQueryString());
        //封装到list集合返回数据
        Map<String,Object> map1 = new HashMap<String, Object>();
        List<Map> maps = new ArrayList<>();
        map1.put("rows",pages.getResult());
        maps.add(map1);
        Map<String,Object> map2 = new HashMap<String, Object>();
        map2.put("currentPage",queryPageBean.getCurrentPage());
        maps.add(map2);
        return new PageResult(pages.getTotal(),maps);

    }

    @Override
    public void delete(Integer id) {
        addressDao.delete(id);
    }

    @Override
    public Address findAddressById(Integer id) {
        return null;
    }

    @Override
    public void update(Address address) {

    }

    @Override
    public List<Address> findAll() {
        //对地址查询优化（因为医院地址一般不会改变所以可以使用redis缓存）
        String address = jedisPool.getResource().get("hosAddress");
        //将地址数据字符串转为java对象
        if (address != null){
            //将redis中查询到的json数据转为list对象返回
            System.out.println("从redis查询");
            JSONArray jsonArray = JSONArray.parseArray(address);
            List<Address> addressList = JSONArray.parseArray(jsonArray.toString(),Address.class);
            return addressList;
        }else {
            System.out.println("从数据库查询");

            List<Address> addressList = addressDao.findAll();
            String hosAddress = JSON.toJSONString(addressList);
            //将数据库中的数据转json保存到redis
            jedisPool.getResource().setex("hosAddress",2,hosAddress);
            return addressList;
        }

    }
}
